package com.dayaokeji.rhythmschoolstudent.client.mine.group;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseFragment;
import com.dayaokeji.rhythmschoolstudent.client.message.GroupChatActivity;
import com.dayaokeji.rhythmschoolstudent.client.mine.group.adapter.GroupListAdapter;
import com.dayaokeji.rhythmschoolstudent.event.UpdateGroupEvent;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.GroupApi;
import com.dayaokeji.server_api.domain.Group;
import com.dayaokeji.server_api.domain.UserInfo;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 *  群组列表
 */
public class GroupListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.rv_group_list)
    RecyclerView rvGroupList;

    private static final GroupApi groupApi = ApiUtils.getApi(GroupApi.class);
    private Call<ServerResponse<List<Group>>> listCall;

    private GroupListAdapter groupListAdapter;

    public static GroupListFragment newInstance() {

        Bundle args = new Bundle();

        GroupListFragment fragment = new GroupListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_group_list,container,false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init() {
        setRecyclerView();
        requestGroupList();
    }

    private void setRecyclerView() {
        rvGroupList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvGroupList.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .colorResId(R.color.color_default_divider)
                .sizeResId(R.dimen.default_divider_height)
                .build());
        groupListAdapter = new GroupListAdapter();
        rvGroupList.setAdapter(groupListAdapter);
        groupListAdapter.setOnItemClickListener(this);
    }


    /**
     * 群组列表
     */
    private void requestGroupList() {
        UserInfo userInfo = UserUtils.getUserInfo();
        if (null == userInfo || TextUtils.isEmpty(userInfo.getId())) {
            ToastUtils.warning("当前为无效用户，请重新登录");
            return;
        }
        listCall = groupApi.list();
        listCall.enqueue(new ServerCallback<List<Group>>() {
            @Override
            public void onResponse(boolean success, ServerResponse<List<Group>> response) {
                if (success) {
                    List<Group> groups = response.getBody();
                    if (getActivity() != null && !getActivity().isFinishing()) {
                        if (groupListAdapter != null) {
                            groupListAdapter.setNewData(groups);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        if (listCall != null) {
            listCall.cancel();
        }
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateData(UpdateGroupEvent updateGroupEvent) {
        requestGroupList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        int size = adapter.getData().size();
        if (position < size) {
            Group group = (Group) adapter.getData().get(position);
            GroupChatActivity.navigation(getActivity(), group.getId(), group.getName());
            ActivityStackManager.finish(getActivity());
        }
    }


}
